import { Inject } from '../decorates/inject';
import { Draw } from './draw';
import { Config } from './config';
import { Header } from './header';
import { Events } from './events';
import { debounceTime, merge } from 'rxjs/operators';
import { CellObj, setValue } from '../utils/catch-cell';
@Inject()
export class Render{
    private readonly defCell = {
        bgClr: '#fff',
        border: {
            top: {
                color: this.conf.headerBorderColor,
                width: 1,
                type: 'thin'
            },
            left: {
                color: this.conf.headerBorderColor,
                width: 1,
                type: 'thin'
            }
        }
    }
    renders: any[];
    constructor(protected draw: Draw, protected conf: Config, protected header: Header, private event: Events) {
        
    }
    public zInit() {
        this.conf.visualAreaChange.pipe(debounceTime(1)).subscribe(() => {
            this.render()
        });
        this.event.windowResizeEvt.pipe(debounceTime(100)).subscribe(() => {
            this.draw.resize();
            setTimeout(() => {
                this.render();
            },10);
        });
    }
    public render(start?: {row: number, col: number}, end?: {row: number, col: number}) {
        let startTime = new Date().getTime();
        if(start == undefined) {
            this.header.drawHeader();
            start = {
                row: this.conf.startRow,
                col: this.conf.startCol
            }
            end = {
                row: this.conf.visualMaxRow,
                col: this.conf.visualMaxCol
            }
        }else{

        }
        for(let i = end.row + 1; i > start.row - 1; i--) {
            for(let j = end.col + 1; j > start.col - 1; j--) {
                setValue({ row: i, col: j});
                this.drawCell(i,j)
            }
        }
        console.log(`渲染时间: ${new Date().getTime() - startTime}ms`)
    }
    public renderCells(start: {row: number, col: number}, end: {row: number, col: number}) {

    }
    protected drawCell(row: number, col: number) {
        let cell = this.conf.getCell(row,col);
        if(cell.row != cell.merge.row || cell.col != cell.merge.col) {
            return ;
        }
        // if(cell.merge.row != cell.row - cell.merge.rowspan + 1 && cell.merge.col != cell.col -cell.merge.colspan + 1) {
        //     return;
        // }else{
        //     if(cell.merge.rowspan != 1 || cell.merge.colspan != 1) {
        //         cell = this.conf.getCell(cell.merge.row,cell.merge.col);
        //     }
        // }
        // if(cell.row >= 1 && cell.row <=2 && cell.col >= 1 && cell.col <= 2 ) {
        //     console.error(cell)   
        // }
        let startY = this.conf.getRowHeight(this.conf.startRow, row) + (this.conf.rowHeaderHeight + 1) + row - this.conf.startRow;
        let startX = this.conf.getColWidth(this.conf.startCol, col) + (this.conf.colHeaderWidth + 1)  + col - this.conf.startCol;
        let colWidth = this.conf.getColWidth(cell.col, cell.merge.col + cell.merge.colspan) + cell.merge.colspan - 1;
        let rowHeight = this.conf.getRowHeight(cell.row, cell.merge.row + cell.merge.rowspan) + cell.merge.rowspan - 1;
        try {
            // if(row == this.conf.startRow + 1 &&  col == this.conf.startCol + 1) {
            //     // console.log(cell);
            //     (<any>cell).fill.color = 'red';
            //     (<any>cell).font.content = '123';
            // }
            (<any>cell).font.content = String(row) + ':' + String(col)
            if(cell != undefined) {
                // if(row ==  5 && col  == 5){
                //     console.log(startX + 1, startY + 1);
                // }
                this.draw.rect(startX + 1, startY + 1, colWidth + 1, rowHeight + 1, <any>cell.font, <any>{bgClr: cell.fill.color ,border: cell.border});
            } else {
                this.draw.rect(startX + 1, startY + 1, colWidth + 1, rowHeight + 1, null, <any>this.defCell);
            }              
        } catch (error) {
            // console.log(cell)
            // console.log(error)
        }
    }
    public loadRender() {

    }
}